home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 611 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.8 KB  |  158 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: "John I. Moore, Jr." <70672.1744@compuserve.com>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: 'const' in header files
  5. Date: 01 Mar 1996 17:39:58 PST
  6. Organization: SoftMoore Consulting
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <31379F8F.7659@compuserve.com>
  9. References: <AD5A0C5196681CA0D@sleipner.nts.mh.se>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Fri, 01 Mar 1996 20:08:31 -0500
  12. X-Mailer: Mozilla 2.0 (Win95; U)
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMTenEUy4NqrwXLNJAQGkigIAmrBgqes3l7nYrA8j4yszoV3jB/zqA2Wv
  15.     Gujcq44bOSnPkIsSBHI3moxBg6l8j3FaOB98XXoCkhHAz7P8gW91cg==
  16.     =d4zk
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Lars Farm wrote:
  20. > Experts explained that this is as it should be for const float, but not for
  21. > const int (I know that the std does not talk about warnings). The point is:
  22. > The experts seems to find some kind of conceptual difference between named
  23. > constant floats and named constant ints, such that declaring const int K =
  24. > L; in a header is considered good practice but const float X = Y; in a
  25. > header is not and justifies a warning. I don't think there should be any
  26. > such difference.
  27.  
  28. I think that there may be some misunderstanding about what a const data 
  29. member actually represents.  In general, a const defined at file scope can be 
  30. of any type.  In C++, it is generally preferable to use const instead of 
  31. #define.
  32.  
  33.     const int maxSize = 100;   //preferable to "#define maxSize 100"
  34.  
  35. Now consider const data members of a class.  First let's distinguish between 
  36. const data members and static const data members.  Const (non-static) data 
  37. members are certainly allowed in a class, but they are const relative to an 
  38. individual object.  Each object can have a different value for the const data 
  39. member.  Consider the following:
  40.  
  41.     class X
  42.       {
  43.         public:
  44.             X(int);
  45.             ...   // other members
  46.  
  47.         private:
  48.             const int m_n;
  49.             ...   // other members
  50.       };
  51.  
  52. We could define the constructor as follows:
  53.  
  54.     X::X(int n)
  55.       : m_n(n)
  56.       {
  57.         ...   // other statements
  58.       }
  59.  
  60. Thus declarations such as 
  61.  
  62.     X x1(5), x2(8);
  63.  
  64. would create two separate objects of class X, each having different values
  65. of the const data member.  The data member is const after construction of the 
  66. object.
  67.  
  68. Now let's address data members which are const and static.  Such a data 
  69. member would be have the same const value for all objects of the class.  
  70. Consider the following:
  71.  
  72.     class X
  73.       {
  74.         public:
  75.             X();
  76.             ...   // other members
  77.  
  78.         private:
  79.             static const int m_n;
  80.             ...   // other members
  81.       };
  82.  
  83. The static const data member can be defined and initialized once outside the 
  84. class definition, usually in a separate source code file (rather than the 
  85. header file).
  86.  
  87.     const X::m_n = 3;
  88.  
  89. Now all objects of the class see the same value (3) for the static const data 
  90. member.
  91.  
  92. All of the above discussion applies to the C++ described in Stroustrup's 
  93. 1991 book as well as to the current draft standard.  What has changed with 
  94. the standard is the ability to actually define and initialize the static 
  95. const data member within the class definition.
  96.  
  97.     class X
  98.       {
  99.         public:
  100.             X();
  101.             ...   // other members
  102.  
  103.         private:
  104.             static const int m_n = 3;
  105.             ...   // other members
  106.       };
  107.  
  108. Before this modification, one had to use enums within the class definition
  109.  
  110.     class X
  111.       {
  112.         public:
  113.             X();
  114.             ...   // other members
  115.  
  116.         private:
  117.             enum { int m_n = 3 };
  118.             ...   // other members
  119.       };
  120.  
  121. or else one had to use a constant declared at file scope
  122.  
  123.     const int m_n = 3;
  124.  
  125.     class X
  126.       {
  127.         public:
  128.             X();
  129.             ...   // other members
  130.  
  131.         private:
  132.             ...   // other members
  133.       };
  134.  
  135. The restriction placed in the standard is that the ability to define and 
  136. initialize the static const data member within the class definition is 
  137. restricted to integral types.  You can't do it, for example, with doubles or 
  138. user-defined types.  As for the reasoning behind this restriction, you'll 
  139. have to ask someone on the standardization committee, but I suspect that it 
  140. has something to do with efficiency.  By the way, you should know that 
  141. Stroustrup opposed this modification even for integral types since it could 
  142. be accomplished within the existing language by the use of enums as 
  143. illustrated above.
  144.  
  145. -- 
  146. John I. Moore, Jr.      phone:  (301) 924-0680
  147. SoftMoore Consulting    email:  70672.1744@compuserve.com
  148. 16233 Monty Court
  149. Rockville, MD  20853-1344
  150. ---
  151. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  152.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  153.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  154.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  155.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  156. ]
  157.